package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Customer;
import com.sjlh.centurylianhua.entity.PurchaseLog;
import com.sjlh.centurylianhua.mapper.CustomerMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerMapper customerMapper;

    /**
     * 分页查询
     */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon searchCon) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isnotNull(searchCon.getContacts()),"contacts",searchCon.getContacts());
        wrapper.like(StringUtils.isnotNull(searchCon.getCusName()),"cusName",searchCon.getCusName());
        wrapper.like(StringUtils.isnotNull(searchCon.getPhone()),"phone",searchCon.getPhone());
        wrapper.eq(StringUtils.isnotNull(searchCon.getState()), "state", searchCon.getState());
        // 封装分页对象
        //判断是否分页
        if(searchCon.getPage()==null){
            wrapper.eq( "customer.state", 1);
            List<Customer> list = customerMapper.selectList(wrapper);
            long count = customerMapper.selectList(wrapper).size();
            ResultData rs=new ResultData(true,20000,"成功",list,count);
            return rs;
        }else {
            // 封装分页对象
            Page<Customer> page = new Page<Customer>(searchCon.getPage(), searchCon.getLimit());
            Page<Customer> customerPage = customerMapper.selectPage(page, wrapper);
            List<Customer> list=customerPage.getRecords();//所有数据
            long count=customerPage.getTotal();//总条数
            ResultData rs=new ResultData(true,20000,"成功",list,count);
            return rs;
        }
    }
    /**
     *
     * 精确查询
     * */
    public Customer getOne(@RequestBody Customer customer){
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        wrapper.eq("cusName",customer.getCusName());
        return customerMapper.selectOne(wrapper);
    }
    /**
     * 添加
     */
    @RequestMapping("/add")
    public Object add(@RequestBody Customer customer) {
        Customer c = this.getOne(customer);
        if (c == null) {
            int rs = customerMapper.insert(customer);
            if (rs == 1)
                return new ResultData(true, 20000, "添加成功！", true, null);
            else
                return new ResultData(false, 20000, "添加失败！", false, null);
           } else{
                return new ResultData(false, 20001, "客户编号已存在！", false, null);

        }
    }
    /**
     * 编辑
     */
    @RequestMapping("/set")
    public Object update(@RequestBody Customer customer){
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        wrapper.eq("cusName",customer.getCusName());
        wrapper.ne("customerId",customer.getCustomerId());
        Customer c = customerMapper.selectOne(wrapper);
        if (c==null){
        int rs = customerMapper.updateById(customer);
            if(rs==1)
            return new ResultData(true,20000,"修改成功！",true,null);
        else
            return new ResultData(false,20000,"修改失败！",false,null);
        } else {
            return new ResultData(false, 20001, "客户编号已存在！", false, null);
        }
    }
    /**
     * 删除
     */
    @RequestMapping("/del")
    public  Object del(@RequestBody Customer customer){
        int rs=customerMapper.deleteById(customer.getCustomerId());
        if(rs==1)
            return new ResultData(true,20000,"删除成功！",true,null);
        else
            return new ResultData(false,20001,"删除失败！",false,null);
    }

}
